package com.hsylient.demo02;

import java.sql.*;

public class TestSelect {
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    @SuppressWarnings("all")
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            statement = connection.createStatement();
            String sql = "select * from user";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.printf("id:%s \t", resultSet.getString("id"));
                System.out.printf("username:%s \t", resultSet.getString("username"));
                System.out.printf("password:%s \t", resultSet.getString("password"));
                System.out.println();
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
